package com.day5.test6_system.service;

import com.day5.test6_system.dao.BaseStudentDao;
import com.day5.test6_system.dao.OtherStudentDao;
import com.day5.test6_system.domain.Student;
import com.day5.test6_system.factory.StudentDaoFactory;

/**
 * @author 小坡
 * @date 2022/8/20  12:14
 * 业务员 主要是负责一些业务的处理
 **/
public class StudentService {
//    创建StudentDao（库管）
    //    private OtherStudentDao studentDao = new OtherStudentDao();

    //   通过学生库管工厂类，获取库管对象
    private BaseStudentDao studentDao = StudentDaoFactory.getStudentDao();

    public boolean addStudent(Student stu) {
//        studentDao.addStudent(stu);    这行代码就是存在学生录入信息的错误bug
//        StudentDao studentDao = new StudentDao();
//        OtherStudentDao studentDao = new OtherStudentDao();
        return studentDao.addStudent(stu);
    }

    public boolean isExists(String id) {
        Student[] students = studentDao.findAllStudent();
//        假设id在数组之中不存在的
        boolean exists = false;
        for (int i = 0; i < students.length; i++) {
            Student student = students[i];
            if (student != null && student.getId().equals(id)) {
                exists = true;
                break;
            }
        }
        return exists;
    }

    public Student[] findAllStundet() {
        Student[] allStudent = studentDao.findAllStudent();
//        思路：数组中只要存在一个不是null 的元素 那就代表又学生的信息
        boolean flag = false;
        for (int i = 0; i < allStudent.length; i++) {
            Student student = allStudent[i];
            if (student != null) {
                flag = true;
                break;
            }
        }
        if (flag) {
            return allStudent;
        } else {
            return null;
        }
    }

    public void delStudent(String delid) {
        studentDao.delStudent(delid);
    }

    public void updateStudent(String updateid, Student newstu) {
        studentDao.updateStudent(updateid, newstu);
    }
}
